#!/bin/sh
STAGED_FILES=$(git diff --cached --name-only| grep ".jsx\{0,1\}$")
if [[ "$STAGED_FILES" = "" ]]; then
   echo "No js file changed"
  exit 0
fi
PASS=true
echo -e "\n Validating Javascript:\n"
# Check for eslint
which ./node_modules/.bin/eslint &> /dev/null
if [[ "$?" == 1 ]]; then
  echo "\t\033[41mPlease install ESlint\033[0m"
  exit 1
fi

./node_modules/.bin/eslint $STAGED_FILES
if [[ "$?" == 1 ]]; then
  PASS=false
fi
echo -e "\nJavascript validation completed!\n"
if ! $PASS; then
  echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again.\n"
  exit 1
else
  echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit $?